/**
 * 
 */
package com.dream.house.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.servyou.base.exception.ServiceException;
import cn.com.servyou.utils.CoreUtils;
import cn.com.servyou.utils.Page;

import com.dream.house.entity.DocumentDTO;
import com.dream.house.entity.GroupDTO;
import com.dream.house.entity.UserDTO;
import com.dream.house.service.DocumentService;
import com.dream.house.service.UserService;
import com.dream.house.utils.Constants;

/**
 * @author ahk
 *
 */
@Controller
@RequestMapping("nrgl")
public class DocController {
	int pageNo=1;
	int pageSize=30;
	
	@Autowired
	DocumentService docService;

	@Autowired
	UserService userService;
	
	@RequestMapping("doc_list")
	public String query(Model model,HttpServletRequest req,String pageNo,String pageSize,String group_id,String doc_type,String title){
		UserDTO user = getUser(req.getSession());
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		Map<String,Object> condition=new HashMap<String,Object>();
		if(group_id!=null){
			condition.put("group_id", group_id);
			model.addAttribute("group_id",group_id);
		}
		if(!user.isAdmin()){
			condition.put("group_id", user.getGroupId());
			model.addAttribute("group_id",user.getGroupId());
		}
		
		if(!CoreUtils.strIsNull(doc_type)){
			condition.put("doc_type", doc_type);
			model.addAttribute("doc_type",doc_type);
		}
		if(!CoreUtils.strIsNull(title)){
			condition.put("title", title);
			model.addAttribute("title",title);
		}
		
		Page page = docService.findByPage(condition, Integer.valueOf(pageNo==null?this.pageNo+"":pageNo), Integer.valueOf(pageSize==null?this.pageSize+"":pageSize));
		model.addAttribute("page", page);
		
		return "nrgl/doc_list";
	}
	
	@RequestMapping("save")
	public String save(Model model,DocumentDTO dto, HttpServletRequest req) {
		try {
			dto.setCreate_user(getUser(req.getSession()).getUserId());
			docService.saveDoc(dto);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return query(model,req,null,null,null,null,null);
	}
	
	@RequestMapping("gotodoc")
	public String doc(Model model, HttpSession session) {
			UserDTO user = getUser(session);
			List<GroupDTO> groupList = userService.getGroups(user.getUserId());
			model.addAttribute("groupList", groupList);
			
		return "nrgl/doc";
	}
	
	@RequestMapping("editdoc/{id}")
	public String editdoc(@PathVariable String id, Model model,HttpSession session) {
		UserDTO user = getUser(session);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		
		DocumentDTO dto=docService.getDoc(id);
		model.addAttribute("doc",dto);
		
		return "nrgl/editdoc";
	}
	
	@RequestMapping("deletedoc/{id}")
	public String deletedoc(@PathVariable String id, Model model,HttpServletRequest req) {
		try {
			docService.removeDoc(id);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		
		return query(model,req,null,null,null,null,null);
	}
	
	public UserDTO getUser(HttpSession session){
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		return user;
	}
	
}
